﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace XMLViewer
{
    public partial class Page : UserControl
    {

        public Page()
        {
            InitializeComponent();
        }

        private void LoadBtn_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            XDocument _xml = new XDocument();
            bool? okClicked;

            openFile.Filter = "XML Files (.xml)|*.xml";
            openFile.Filter += "|All Files (*.*)| *.*";
            openFile.FilterIndex = 1;
            openFile.Multiselect = false;

            okClicked = openFile.ShowDialog();

            if (okClicked == true)
            {
                System.IO.Stream file = openFile.File.OpenRead();

                RootNode.Reset();
                //all child XmlNodeDisplay controls will derive from these settings
                RootNode.MainStackPanel.MinWidth = 410;
                RootNode.MainStackPanel.MaxWidth = 410;

                try
                {
                    _xml = XDocument.Parse(new StreamReader(file).ReadToEnd());

                    RootNode.Node = _xml;
                }
                catch
                {
                    RootNode.Node = XDocument.Parse("<InvalidFileFormat/>");
                }

                file.Close();
            }

        } 
    }
}
